﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cstdx.Proto.Std;

/// <summary>
/// 财务信息工具类
/// </summary>
public static class FinanceInfoHelper
{
	/// <summary>
	/// 创建财务信息请求
	/// </summary>
	/// <param name="market">市场类型</param>
	/// <param name="code">股票代码</param>
	/// <returns>请求实例</returns>
	public static FinanceInfoRequest NewFinanceInfoRequest(Exchange.MarketType market, string code)
	{
		// 初始化未知字段（对应Go中的HexString2Bytes("0c 1f 18 76 00 01 0b 00 0b 00 10 00 01 00")）
		byte[] unknown1 = HexStringToBytes("0c 1f 18 76 00 01 0b 00 0b 00 10 00 01 00");

		return new FinanceInfoRequest
		{
			Unknown1 = unknown1,
			Market = market,
			Code = code
		};
	}

	/// <summary>
	/// 创建财务信息请求和响应实例
	/// </summary>
	/// <param name="market">市场类型</param>
	/// <param name="code">股票代码</param>
	/// <returns>请求、响应实例和可能的错误</returns>
	public static (FinanceInfoRequest request, FinanceInfoResponse response, Exception error) NewFinanceInfo(Exchange.MarketType market, string code)
	{
		try
		{
			var request = NewFinanceInfoRequest(market, code);
			var response = new FinanceInfoResponse();
			return (request, response, null);
		}
		catch (Exception ex)
		{
			return (null, null, ex);
		}
	}

	/// <summary>
	/// 16进制字符串转字节数组（复用之前的转换逻辑）
	/// </summary>
	private static byte[] HexStringToBytes(string hexString)
	{
		hexString = hexString.Replace(" ", "");

		if (hexString.Length % 2 != 0)
		{
			throw new ArgumentException("无效的16进制字符串", nameof(hexString));
		}

		byte[] bytes = new byte[hexString.Length / 2];
		for (int i = 0; i < bytes.Length; i++)
		{
			string hex = hexString.Substring(i * 2, 2);
			bytes[i] = Convert.ToByte(hex, 16);
		}

		return bytes;
	}
}


